import Vue from 'vue';
import VueRouter from 'vue-router';
/*
 * import { _ } from 'vue-underscore';
 * import axios from 'axios';
 */


const OuterSpace = () => import( /* webpackChunkName: "basic" */ '@/components/404.vue');
const Main = () => import( /* webpackChunkName: "main" */ '@/views/layout');


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

Vue.use(VueRouter);

export let asyncRoutes  = [
{
    path: '/main',
    component: Main,
    children: [
        {
            path: 'signIn',
            name:'考勤打卡',
            component: () => import( /* webpackChunkName: "main" */ '@/views/sign')
        },
        {
            path: 'signInStatistics',
            name:'签到统计',
            component: () => import( /* webpackChunkName: "basic" */ '@/views/signInStatistics')
        },
        {
            path: 'reviewWorkOrder',
            name:'外勤审批',
            component: () => import( /* webpackChunkName: "basic" */ '@/views/reviewWorkOrder')
        },
        {
            path: 'workAttendanceCardApply',
            name:'补卡申请',
            component: () => import( /* webpackChunkName: "basic" */ '@/views/workAttendanceCardApply')
        },
        {
            path: 'test2',
            component: () => import( /* webpackChunkName: "basic" */ '@/views/test2')
        },
    ]
},
{
    path: '/',
    redirect: '/main/signIn'
},
{
    path: '/notFound',
    component: OuterSpace
},
{
    path: '/signInByTakePhone',
    name: '拍照打卡',
    component: () => import( /* webpackChunkName: "main" */ '@/views/sign/signInByTakePhone.vue')
},
{
    path: '/test',
    component: () => import( /* webpackChunkName: "basic" */ '@/views/test')
},
{
    path: '/test1',
    component: () => import( /* webpackChunkName: "basic" */ '@/views/test1')
},
// {
//     path: '/test2',
//     component: () => import( /* webpackChunkName: "basic" */ '@/views/test2')
// },
    {
        path: '/test3',
        name: '考勤打卡',
        component: () => import( /* webpackChunkName: "main" */ '@/views/sign/test')
    },
]

const router = new VueRouter({
    mode: 'hash',
    routes: asyncRoutes
});


router.afterEach((to) => {
    window.localStorage.setItem('currentRouter', to.fullPath);
    // let accessToken = to.query.access_token || localStorage.getItem('access_token');
    // if (accessToken != 'undefined' && accessToken != undefined && accessToken != '' && accessToken != null){
    //     window.localStorage.setItem('access_token', accessToken);
    //     asyncRoutes.forEach(element => {
    //         if (element.path == '/main') {
    //             let pageIndex = element.children.findIndex(v=>v.path=='page');
    //             // console.log(pageIndex);
    //             // if (pageIndex==-1){ 
    //             //     element.children.push({
    //             //         path: 'page',
    //             //         component: () =>
    //             //             import( /* webpackChunkName: "login" */ '@/views/chnlayout/content.vue')
    //             //     });
    //             // }
    //         }
    //     });
    // }else{
    //     asyncRoutes.forEach((element,index) => {
    //         if (element.path == '/main') {
    //             element.children.forEach(child=>{
    //                 if (child.path=='page'){
    //                     element.children.splice(index,1);
    //                 }
    //             })
    //         }
    //     });
    // }
    for (let i = 0; i < asyncRoutes.length; i++) {
        const addRoute = asyncRoutes[i]
        router.addRoute(addRoute)
    }
    // if (to.path !== '/login' && to.path !== '/main/page') {
    //     let userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    //     let showInfo = '';
    //     if(userInfo.employeeName!=undefined && userInfo.employeeName!=null && userInfo.employeeName!=''){
    //         showInfo += userInfo.employeeName;
    //     }
    //     if(userInfo.username!=undefined && userInfo.username!=null && userInfo.username!=''){
    //         showInfo += userInfo.username;
    //     }
	//     watermark.set(showInfo)
	// }
});

export default router;
